package com.yungu.swift.api.route.v1.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.google.common.collect.Lists;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.utils.FileUploadUtil;
import com.yungu.swift.api.utils.RandomUtil;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.assets.passenger.model.dto.PassengerAddressDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerCouponDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerDto;
import com.yungu.swift.assets.passenger.model.param.ApiPasPayCouponParam;
import com.yungu.swift.assets.passenger.model.param.ApiPassengerAddressParam;
import com.yungu.swift.assets.passenger.model.vo.PassengerVo;
import com.yungu.swift.assets.passenger.service.PassengerAddressService;
import com.yungu.swift.assets.passenger.service.PassengerCouponService;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.base.model.api.ApiBusinessPageParam;
import com.yungu.swift.base.model.api.ApiPageParam;
import com.yungu.swift.common.model.dto.CommonFeedbackDto;
import com.yungu.swift.common.model.dto.CommonIdentifyDto;
import com.yungu.swift.common.service.CommonFeedbackService;
import com.yungu.swift.common.service.CommonSmsService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.constants.PassengerConstant;
import com.yungu.swift.order.model.dto.OrderEvaluateDto;
import com.yungu.swift.order.service.OrderEvaluateService;
import com.yungu.swift.system.marketing.model.dto.MarketingGiftDto;
import com.yungu.swift.system.marketing.model.vo.MarketingShareVo;
import com.yungu.swift.system.marketing.service.MarketingCouponService;
import com.yungu.swift.system.marketing.service.MarketingGiftService;
import com.yungu.swift.system.marketing.service.MarketingShareService;
import com.yungu.swift.system.sys.model.dto.SysAccountPassengerDto;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import com.yungu.swift.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

import static com.yungu.swift.api.config.NacosListener.SYS_CONFIG;

/**
 * Description:UserInfoApi
 * Author: Zerrion
 * Date:2016/12/12 16:35
 * Version:1.0
 */
@Controller
@RequestMapping(value = "/api/v1/passenger")
@Api(value = "【乘客】【用户信息】乘客信息接口", tags = "乘客信息相关操作")
public class UserInfoApi extends BaseApi {

    @Reference
    private PassengerService passengerService;
    @Reference
    private PassengerAddressService passengerAddressService;
    @Reference
    private PassengerCouponService passengerCouponService;
    @Reference
    private CommonFeedbackService commonFeedbackService;
    @Reference
    private DriverService driverService;
    @Reference
    private OrderEvaluateService orderEvaluateService;
    @Reference
    private MarketingShareService marketingShareService;
    @Reference
    private MarketingCouponService marketingCouponService;
    @Reference
    private MarketingGiftService marketingGiftService;
    @Reference
    private SysAccountPassengerService SysAccountPassengerService;
    @Reference
    private CommonSmsService commonSmsService;

    @ResponseImgOverride
    @ResponseBody
    @ApiOperation(value = "乘客信息接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/user/info"}, method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData<PassengerVo> info(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        PassengerVo passengerVo = passengerService.selInfo(getUserUid(request)).getData();
        if (passengerVo == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取用户失败-该用户不存在");
        }
        return ResponseData.buildSuccessResponse("获取用户信息成功", passengerVo);
    }

    @ResponseImgOverride
    @ResponseBody
    @ApiOperation(value = "乘客信息设置接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/user/setInfo"}, method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData<PassengerVo> setInfo(
            @ApiParam(name = "token", value = "token", required = true) @RequestParam String token,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "face", value = "头像") @RequestParam(value = "face", required = false) String face,
            @ApiParam(name = "nickName", value = "称呼 + 性别  (例）张先生，王女士") @RequestParam(value = "nickName", required = false) String nickName,
            @ApiParam(name = "sex", value = "性别 1：女，2：男") @RequestParam(value = "sex", required = false) Integer sex,
            @ApiParam(name = "passengerUuid", value = "用户uuid") @RequestParam(value = "passengerUuid", required = false) String passengerUuid,
            HttpServletRequest request) {
        Date date = new Date();
        SysAccountPassengerDto SysAccountPassengerDto = new SysAccountPassengerDto();
        SysAccountPassengerDto.setUuid(getUserUid(request));
        SysAccountPassengerDto.setAccountName(nickName);
        SysAccountPassengerDto.setUpdateOn(date);
        ResponseData<Boolean> responseData = SysAccountPassengerService.edit(SysAccountPassengerDto);
        if (!responseData.isSuccess() || responseData.getData() == null || Objects.equals(responseData.getData(), Boolean.FALSE)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "修改乘客账户账户名称失败！");
        }
        PassengerDto passengerDto = new PassengerDto();
        passengerDto.setUpdateOn(date);
        passengerDto.setNickname(nickName);
        passengerDto.setSex(sex);
        passengerDto.setUuid(getUserUid(request));
        passengerDto.setFace(face);
        passengerDto.setToken(token);
        passengerService.edit(passengerDto);
        return passengerService.selInfo(getUserUid(request));
    }

    @ApiOperation(value = "保存乘客信息", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/user/saveSettings"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Boolean> saveSettings(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "avatar", value = "头像", required = true) @RequestParam MultipartFile avatar,
            @ApiParam(name = "nickName", value = "称呼 + 性别  (例）张先生，王女士") @RequestParam(value = "nickName", required = false) String nickName,
            @ApiParam(name = "sex", value = "性别 1：女，2：男") @RequestParam(value = "sex", required = false) Integer sex,
            HttpServletRequest request) {
        PassengerVo passengerVo = passengerService.selInfo(getUserUid(request)).getData();
        ResponseData responseData = FileUploadUtil.uploadImage(avatar, appid, passengerVo.getMobile(), "avatar");
        if (!responseData.isSuccess()) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "上传头像失败！");
        }
        String face = responseData.getData().toString();
        PassengerDto passenger = new PassengerDto();
        passenger.setUpdateOn(new Date());
        passenger.setNickname(nickName);
        passenger.setSex(sex);
        passenger.setUuid(getUserUid(request));
        passenger.setFace(face);
        return passengerService.edit(passenger);
    }

    @ApiOperation(value = "乘客地址信息接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/address/list"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<PassengerAddressDto>> addressList(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        return passengerAddressService.addressList(getUserUid(request));
    }

    @ApiOperation(value = "乘客设置常用地址接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/address/add"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<PassengerAddressDto> addAddress(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "type", value = "0家 ，1.公司", required = true) @RequestParam Integer type,
            @ApiParam(name = "province", value = "省份") @RequestParam(value = "province", required = false) String province,
            @ApiParam(name = "city", value = "城市") @RequestParam(value = "city", required = false) String city,
            @ApiParam(name = "address", value = "地址") @RequestParam(value = "address", required = false) String address,
            @ApiParam(name = "addressTitle", value = "地址标题") @RequestParam(value = "addressTitle", required = false) String addressTitle,
            @ApiParam(name = "lng", value = "经度") @RequestParam(value = "lng", required = false) Double lng,
            @ApiParam(name = "lat", value = "纬度") @RequestParam(value = "lat", required = false) Double lat,
            @ApiParam(name = "adCode", value = "区域代码") @RequestParam(value = "adCode", required = false) String adCode, HttpServletRequest request) {
        ApiPassengerAddressParam addressParam = new ApiPassengerAddressParam();
        addressParam.setType(type);
        addressParam.setProvince(province);
        addressParam.setCity(city);
        addressParam.setAddressTitle(addressTitle);
        addressParam.setAddress(address);
        addressParam.setLng(lng);
        addressParam.setLat(lat);
        addressParam.setUserUuid(getUserUid(request));
        addressParam.setAdCode(adCode);
        return passengerAddressService.addAddress(addressParam);
    }


    @ApiOperation(value = "乘客获取可用的优惠券接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/coupon/listUsable"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<PassengerCouponDto>> getUseCouponList(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "token", value = "token", required = true) @RequestParam String token,
            @ApiParam(name = "nowPage", value = "页码", required = true) @RequestParam Integer nowPage,
            @ApiParam(name = "tripType", value = "订单类型（空值则默认返回全部可用优惠券）") @RequestParam(value = "tripType", required = false) Integer tripType) {
        ApiBusinessPageParam businessPageParam = new ApiBusinessPageParam();
        businessPageParam.setBusinessType(tripType);
        businessPageParam.setNowPage(nowPage);
        businessPageParam.setToken(token);
        return passengerCouponService.getUseCouponList(businessPageParam);
    }


    @ApiOperation(value = "乘客支付时选择可用优惠券接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/coupon/listPay"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<PassengerCouponDto>> getPayCouponList(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "typeTrip", value = "订单类型", required = false) @RequestParam(required = false) Integer typeTrip,
            @ApiParam(name = "typeModule", value = "订单类型", required = false) @RequestParam(required = false) Integer typeModule,
            @ApiParam(name = "totalFare", value = "订单总金额", required = true) @RequestParam Double totalFare,
            @ApiParam(name = "originCityUuid", value = "出发城市uuid", required = true) @RequestParam String originCityUuid,
            HttpServletRequest request) {
        ApiPasPayCouponParam pasPayCouponParam = new ApiPasPayCouponParam();
        pasPayCouponParam.setUserUuid(getUserUid(request));
        pasPayCouponParam.setBusinessType(typeTrip == null ? typeModule : typeTrip);
        pasPayCouponParam.setTotalFare(totalFare);
        pasPayCouponParam.setOriginCityUuid(originCityUuid);
        return passengerCouponService.getPayCouponList(pasPayCouponParam);
    }

    @ApiOperation(value = "乘客修改优惠券状态接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/coupon/release"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData release(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "couponId", value = "优惠券id", required = true) @RequestParam String couponId,
            HttpServletRequest request) {
        PassengerCouponDto passengerCouponDto = new PassengerCouponDto();
        passengerCouponDto.setUuid(couponId);
        passengerCouponDto.setStatus(PassengerConstant.COUPON_STATUS_VALID);
        passengerCouponDto.setUpdateOn(new Date());
        passengerCouponDto.setUpdateBy(getUserUid(request));
        return passengerCouponService.edit(passengerCouponDto);
    }


    @ApiOperation(value = "乘客上传图像接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/user/uploadImage"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<String> uploadImage(
            @ApiParam(name = "token", value = "token", required = false) @RequestParam(required = false) String token,
            @ApiParam(name = "sign", value = "签名", required = false) @RequestParam(required = false) String sign,
            @ApiParam(name = "appid", value = "APP标识", required = false) @RequestParam(required = false) String appid,
            @ApiParam(name = "noncestr", value = "随机数", required = false) @RequestParam(required = false) String noncestr,
            @ApiParam(name = "avatar", value = "头像", required = true) @RequestPart MultipartFile avatar,
            HttpServletRequest request) {

        String fileName = avatar.getOriginalFilename();
        String lastName = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (!lastName.equals(MagicConstant.PICTURE_SUFFIX_JPG)
                && !lastName.equals(MagicConstant.PICTURE_SUFFIX_PNG)
                && !lastName.equals(MagicConstant.PICTURE_SUFFIX_JPEG)
                && !lastName.equals(MagicConstant.PICTURE_SUFFIX_BMP)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "图片格式不正确，请重新选择！");
        }

        //上传用户头像
        PassengerVo passengerVo = passengerService.selInfo(getUserUid(request)).getData();
        return FileUploadUtil.uploadImage(avatar, appid, passengerVo.getMobile(), "avatar");
    }


    @RequestMapping(value = {"/token/feedback/add"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "添加反馈接口", response = ResponseData.class)
    public ResponseData<Boolean> addFeedback(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "title", value = "反馈标题", required = false) @RequestParam(required = false) String title,
            @ApiParam(name = "content", value = "反馈内容", required = true) @RequestParam String content,
            HttpServletRequest request) {
        CommonFeedbackDto feedbackDto = new CommonFeedbackDto();
        feedbackDto.setUuid(StringUtils.buildUUID());
        feedbackDto.setAppid(appid);
        feedbackDto.setContent(content);
        feedbackDto.setTitle(content);
        feedbackDto.setIdentity(CommonConstant.PASSENGER);
        feedbackDto.setUserUuid(getUserUid(request));
        feedbackDto.setCreateOn(new Date());
        feedbackDto.setStatus(CommonFeedbackDto.STATUS_UNREPLY);
        return commonFeedbackService.add(feedbackDto);
    }

    @RequestMapping(value = {"/token/feedback/list"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取反馈列表接口", response = ResponseData.class, notes = "list")
    public ResponseData<PageList<CommonFeedbackDto>> findFeedbackList(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "nowPage", value = "页码", required = true) @RequestParam Integer nowPage,
            HttpServletRequest request) {
        ApiPageParam pageParam = new ApiPageParam();
        pageParam.setNowPage(nowPage);
        Map<String, Object> map = MapUtils.build(3);
        map.put("userUuid", getUserUid(request));
        map.put("identity", CommonConstant.PASSENGER);
        return commonFeedbackService.queryPage(map, pageParam);
    }

    @ApiOperation(value = "乘客获取家/公司地址接口", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/address/info"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData addressInfo(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "type", value = "地址类型type: 2 家地址 3 公司地址", required = true) @RequestParam Integer type,
            HttpServletRequest request) {
        if (StringUtils.isNull(type)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "请填写地址类型！");
        }
        PassengerAddressDto passengerAddressDto = passengerAddressService.getAddressInfo(getUserUid(request), type).getData();
        if (passengerAddressDto != null) {
            return ResponseData.buildSuccessResponse("获取地址信息成功", passengerAddressDto);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "未检索到相应的地址信息");
    }

    @RequestMapping(value = {"/token/driInfo"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "乘客端获取司机信息详情接口", notes = "个人信息/星级评价/乘客评价", response = ResponseData.class)
    public ResponseData<Map<String, Object>> driInfo(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "driverUuid", value = "司机Uuid", required = true) @RequestParam String driverUuid) {
        if (StringUtils.isEmpty(driverUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "司机的编号不能为空");
        }
        return driverService.getDriverInfo(driverUuid);
    }

    @RequestMapping(value = {"/token/driEvaluateList"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "乘客端获取司机评价列表接口", notes = "乘客端获取司机评价列表", response = ResponseData.class)
    public ResponseData driEvaluateList(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "driverUuid", value = "司机Uuid", required = true) @RequestParam String driverUuid,
            @ApiParam(name = "nowPage", value = "当前页", required = false) @RequestParam(required = false) Integer nowPage) {
        if (StringUtils.isEmpty(driverUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "司机的编号不能为空");
        }
        List<OrderEvaluateDto> oes = orderEvaluateService.getListOfEvaluate(driverUuid, new ApiPageParam(nowPage)).getData();
        if (oes == null) {
            return ResponseData.buildSuccessResponse("该司机尚无评价信息", Collections.emptyList());
        }
        return ResponseData.buildSuccessResponse("获取成功", oes);
    }

    @RequestMapping(value = {"/token/marketing/isOpenCity"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "判断乘客位置是否开通乘客邀请好友城市", notes = "获取乘客邀请好友开通城市", response = ResponseData.class)
    public ResponseData<Map<String, Boolean>> isOpenCity(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "token", value = "token", required = false) @RequestParam(required = false) String token,
            @ApiParam(name = "adCode", value = "adCode", required = false) @RequestParam(required = false) String adCode) {
        if (adCode == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "未获取定位点");
        }
        return marketingGiftService.isOpenCity(adCode, 2);
    }

    @RequestMapping(value = {"/token/share/info"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取乘客分享信息", notes = "获取乘客分享信息", response = ResponseData.class)
    public ResponseData listActivity(
            @ApiParam(name = "token", value = "token", required = true) @RequestParam String token,
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid) {
        Map<String, Object> map = MapUtils.build();
        map.put("token", token);

        PassengerDto passengerDto = passengerService.get(map).getData();
        if (passengerDto == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "找不到对应的登录用户");
        }
        String mobile = passengerDto.getMobile();
        map.clear();
        map.put("clientSide", 1);
        map.put("status", 1);
        MarketingShareVo marketingShareVo = marketingShareService.findVo(map).getData();
        //拼接url把邀请人的手机号和邀请人类型拼接到url中
        if (marketingShareVo != null) {
            StringBuilder linkUrlSb = new StringBuilder(SYS_CONFIG.getApiHost());
            linkUrlSb.append(marketingShareVo.getLinkUrl());
            if (marketingShareVo.getLinkUrl().indexOf(MagicConstant.SYMBOL_QUESTION_MARK) != -1) {
                linkUrlSb.append("&");
            } else {
                linkUrlSb.append("?");
            }
            linkUrlSb.append("appid=").append(appid).append("&inviter=").append(mobile).append("&inviterType=1");
            marketingShareVo.setLinkUrl(linkUrlSb.toString());
            marketingShareVo.setLinkTitle(SYS_CONFIG.getName());
            //获取乘客邀请礼包优惠券总额
            BigDecimal passengerMoney = marketingCouponService.getSumMoneyByTypes(Lists.newArrayList(2)).getData();
            if (passengerMoney == null) {
                passengerMoney = new BigDecimal(0);
            }
            marketingShareVo.setInviterMoney(passengerMoney);
            marketingShareVo.setInviteeMoney(passengerMoney);
            map.clear();
            map.put("type", 2);
            map.put("status", 1);
            MarketingGiftDto marketingGiftDto = (marketingGiftService.findGiftByMap(map)).getData();
            if (marketingGiftDto == null) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到数据", null);
            }
            marketingShareVo.setInvitedGiftName(marketingGiftDto.getInvitedGiftName());
            marketingShareVo.setInvitedRewardMethod(marketingGiftDto.getInvitedRewardMethod());
            marketingShareVo.setGetMode(marketingGiftDto.getGetMode());
            return ResponseData.buildSuccessResponse("获取成功", marketingShareVo);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到数据", null);
    }

    @RequestMapping(value = {"/token/phone/checkCode"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "用户旧手机号校验验证码", notes = "用户旧手机号校验验证码", response = ResponseData.class)
    public ResponseData<Boolean> checkCode(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "identifyCode", value = "验证码", required = true) @RequestParam(required = false) String identifyCode,
            @ApiParam(name = "mobile", value = "手机号", required = true) @RequestParam(required = false) String mobile) {
        CommonIdentifyDto commonIdentifyDto = null;
        String decryptMobile = RandomUtil.randomDecrypt(mobile);
        if (!ValidateUtils.validateMobile(decryptMobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        String decryptIdentifyCode = RandomUtil.randomDecrypt(identifyCode);
        if (decryptIdentifyCode.length() != MagicConstant.INT_FOUR) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "请输入正确的验证码");
        }
        String mobileTop3 = decryptMobile.substring(0, 3);
        if (!mobileTop3.equals(MagicConstant.STRING_ONE_FOUR_TWO)) {
            Map<String, Object> paramMap = MapUtils.build(5);
            paramMap.put("mobile", decryptMobile);
            paramMap.put("identifyCode", decryptIdentifyCode);
            paramMap.put("status", CommonConstant.IDENTIFY_CODE_STATUS_UNUSED);
            paramMap.put("appid", appid);
            paramMap.put("type", "1");
            commonIdentifyDto = commonSmsService.get(paramMap).getData();
            if (commonIdentifyDto == null) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "验证码错误");
            }
            if (System.currentTimeMillis() - commonIdentifyDto.getCreateOn().getTime() > 120000) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "验证码已过期，请重新获取");
            }
        }
        return ResponseData.buildSuccessResponse("验证成功", true);
    }

    @RequestMapping(value = {"/token/phone/updatePhone"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "用户新手机号校验验证码并修改手机号", notes = "用户新手机号校验验证码并修改手机号", response = ResponseData.class)
    public ResponseData<Boolean> updatePhone(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "token", value = "用户token", required = true) @RequestParam String token,
            @ApiParam(name = "identifyCode", value = "验证码", required = true) @RequestParam String identifyCode,
            @ApiParam(name = "mobile", value = "手机号", required = true) @RequestParam String mobile) {
        CommonIdentifyDto commonIdentifyDto = null;
        String decryptMobile = RandomUtil.randomDecrypt(mobile);
        if (!ValidateUtils.validateMobile(decryptMobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        String decryptIdentifyCode = RandomUtil.randomDecrypt(identifyCode);
        if (decryptIdentifyCode.length() != MagicConstant.INT_FOUR) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "请输入正确的验证码");
        }
        String mobileTop3 = decryptMobile.substring(0, 3);
        //查询数据库校验验证码
        if (!mobileTop3.equals(MagicConstant.STRING_ONE_FOUR_TWO)) {
            Map<String, Object> paramMap = MapUtils.build(5);
            paramMap.put("mobile", decryptMobile);
            paramMap.put("identifyCode", decryptIdentifyCode);
            paramMap.put("status", CommonConstant.IDENTIFY_CODE_STATUS_UNUSED);
            paramMap.put("appid", appid);
            paramMap.put("type", "1");
            commonIdentifyDto = commonSmsService.get(paramMap).getData();
            if (commonIdentifyDto == null) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "验证码错误");
            }
            if (System.currentTimeMillis() - commonIdentifyDto.getCreateOn().getTime() > 120000) {
                return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "验证码已过期，请重新获取");
            }
            //验证码校验通过后修改用户手机号
            PassengerDto passengerDto = new PassengerDto();
            passengerDto.setToken(token);
            passengerDto.setMobile(decryptMobile);
            return ResponseData.buildSuccessResponse("修改成功", passengerService.editByToken(passengerDto));
        }
        return ResponseData.buildSuccessResponse("修改成功", true);
    }

}
